package cn.operation.backend.service;

import cn.operation.backend.enums.DeleteStatusEnum;
import cn.operation.base.mapper.BmsPayableBillMapper;
import cn.operation.base.mapper.ItfLatestCodePondMapper;
import cn.operation.base.model.BmsPayableBillDO;
import cn.operation.base.model.ItfLatestCodePond;
import cn.operation.base.model.ItfLatestCodePondExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PayableBillServiceImpl implements PayableBillService {
    private static final Logger LOGGER = LoggerFactory.getLogger(PayableBillServiceImpl.class);
    @Autowired
    private BmsPayableBillMapper payableBillMapper;
    @Autowired
    private ItfLatestCodePondMapper itfLatestCodePondMapper;
    @Override
    public BmsPayableBillDO queryBillByCode(String billCode) throws Exception {
        if (billCode == null) {
            LOGGER.error("PayableBillServiceImpl.queryBillByCode param billCode must not be null ");
            throw new IllegalArgumentException("账单号为空");
        }
        BmsPayableBillDO billDO = null;
        try {
            billDO = payableBillMapper.queryBillByCode(billCode);
        } catch (Exception e) {
            LOGGER.error("PayableBillServiceImpl.queryBillByCode param query payableBillDetail data error: ");
            throw new IllegalArgumentException("查询账单明细数据异常:"+ e);
        }
        try {
            ItfLatestCodePondExample example = new ItfLatestCodePondExample();
            example.createCriteria().andCodeEqualTo(billCode).andStatusEqualTo(DeleteStatusEnum.NORMAL.getValue());
            List<ItfLatestCodePond> list = itfLatestCodePondMapper.selectByExample(example);
            if (list.size()>0) {
                ItfLatestCodePond codePond = new ItfLatestCodePond();
                codePond.setId(list.get(0).getId());
                codePond.setStatus(DeleteStatusEnum.DELETRE.getValue());
                itfLatestCodePondMapper.updateByPrimaryKeySelective(codePond);
            }
        } catch (Exception e) {
            LOGGER.error("PayableBillServiceImpl.queryBillByCode param update ItfLatestCodePond data error: ");
            throw new IllegalArgumentException("修改业务单据号码池单据状态为删除状态异常:"+ e);
        }
        return billDO;
    }
}
